package com.wzy.level.category.categoryDaoImpl;

import com.wzy.Jdbc封装.dao.NewsDao;
import com.wzy.Jdbc封装.pojo.News_category;
import com.wzy.level.byPage.byPageDto.ByPage;
import com.wzy.level.category.categoryDao.categoryDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class categoryDaoImpl implements categoryDao {


    @Override
    public List<News_category> selectAll() {
        ArrayList<News_category> news_categories = new ArrayList<>();
        ResultSet resultSet = null;
        try {
            String  sql = "SELECT * FROM `news_category`";
            Object[] obj={};
            resultSet = NewsDao.selectById(sql, obj);
            while(resultSet.next()) {
                News_category news_category = new News_category();

                int id1 = resultSet.getInt(1);
                String name = resultSet.getString(2);
                java.util.Date date = resultSet.getDate(3);

                news_category.setId(id1);
                news_category.setName(name);
                news_category.setDate(date);

                news_categories.add(news_category);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return news_categories;
    }

    @Override
    public News_category selectById(int id) {
        ResultSet resultSet = null;
        News_category news_category = new News_category();
        try {
            String  sql = "SELECT * FROM `news_category` WHERE id =?";
            Object[] obj={id};
            resultSet = NewsDao.selectById0(sql, obj);
            while(resultSet.next()) {
                int id1 = resultSet.getInt(1);
                String name = resultSet.getString(2);
                java.util.Date date = resultSet.getDate(3);

                news_category.setId(id1);
                news_category.setName(name);
                news_category.setDate(date);

            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return news_category;
    }

    @Override
    public List<News_category> selectByPage(ByPage byPage) {
        ResultSet resultSet = null;
        News_category news_category = new News_category();
        ArrayList<News_category> news_categories = new ArrayList<>();
        try {
            String  sql = "SELECT * FROM `news_category`  LIMIT ?,?";
            Object[] obj={byPage};
            resultSet = NewsDao.selectByPage(sql, obj);
            while(resultSet.next()) {
                int id1 = resultSet.getInt(1);
                String name = resultSet.getString(2);
                java.util.Date date = resultSet.getDate(3);

                news_category.setId(id1);
                news_category.setName(name);
                news_category.setDate(date);
                news_categories.add(news_category);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Connection connection=null;
            NewsDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return news_categories;
    }

    @Override
    public int insert(News_category category) {
        int rows = 0;
        String sql = "INSERT INTO `news_category`(`id`,`name`,`createDate`) values(?,?,?)";
        Object[] obj = {category};
        rows = NewsDao.insert(sql, obj);
        return rows;
    }

    @Override
    public int update(News_category category) {
        int rows = 0;
        String sql = "UPDATE `news_category` SET name=? ,createDate=?  WHERE id=?";
        Object[] obj = {category};
        rows = NewsDao.update(sql, obj);
        return rows;
    }

    @Override
    public int deleteById(int id) {
        int rows = 0;
        String sql = "DELETE FROM `news_category`  WHERE id=? ";
        Object[] obj = {id};
        rows = NewsDao.delete(sql, obj);
        return rows;
    }

    @Override
    public int deleteByName(News_category category) {
        return 0;
    }
}
